<?php
namespace jy_creditchange;
defined('BY_JYA') or exit('error');
class member extends \plugin{
	//type 0冻结，1消费解冻，2解冻，3回购
	public function recharge($mid=0,$fee=0,$credittype=0,$type=0,$remark=''){
		$mid=$mid?:$_SESSION['uid'];
		if(!intval($fee)){
			return false;
		}
		$member=pdo_get('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$update=array();
		if($type==0){
			if($member['credit'.$credittype]<$fee){
				exi('余额不足，无法发起！','error');
			}
			$update['credit'.$credittype.' -=']=$fee;
			$update['credit'.$credittype.'_fz +=']=$fee;
			$msg='冻结:￥'.$fee;
		}elseif($type==1){
			$update['credit'.$credittype.'_fz -=']=$fee;
			$msg='兑换解冻:￥'.$fee;
		}elseif($type==2){
			$update['credit'.$credittype.'_fz -=']=$fee;
			$update['credit'.$credittype.' +=']=$fee;
			$msg='解冻:￥'.$fee;
		}elseif($type==3){
			$update['credit'.$credittype.'_fz -=']=$fee;
			$msg='平台回购:￥'.$fee;
		}
		$user=pdo_update('core_members',$update,array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$params=array(
			'msg'=>$msg,
			'status'=>1,
			'mid'=>$mid,
		);
		$this->set_log($params,$type);
		return true;
	}
	/*
		退款
	*/
	public function tuikuan($refund,$noexi=false){
		if($refund['status']!=3){
			if($noexi){
				return false;
			}
			exi('退款单不符合打款要求！','error');
		}
		//支付方式为余额支付，退款至虚拟币
		if($refund['paytype']=='credit'){
			$pay_out_id=model('core/member')->add_credit2($refund['mid'],$refund['fee'],'订单退款');
		}elseif(strpos($refund['paytype'],'credit') !== false){
			$pay_out_id=model('core/member')->member_credit($refund['mid'],$refund['paytype'],$refund['fee'],true,'','订单退款');
		}elseif($refund['paytype']=='wechat'){
			$payinfo=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$refund['pay_id']));
			if(!$refund['transid']){
				//exi('退款失败，没有微信支付号！','error');
			}
			$payinfo['transid']=$refund['transid'];
			$payinfo['money']=$refund['fee'];
			$payinfo['remark']='订单退款';
			$pay_out_id=model('core/member')->refund_out($refund['mid'],$payinfo,$noexi);
		}else{
			$params=array(
				'money'=>$refund['fee'],
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->pay_out($refund['mid'],$params,$noexi);
		}
		$update=array(
			'status'=>4,
			'pay_out_id'=>$pay_out_id,
		);
		pdo_update('jy_creditchange_order',$update,array('id'=>$refund['id'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	/*
		日志信息
	*/
	public function set_log($params,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'msg'=>$params['msg'],
			'status'=>$params['status'],
			'type'=>$type,
			'createtime'=>TIMESTAMP
		);
		$this->pdo_insert('log',$insert);
		return true;
	}
}